package com.king.modules.system.domain;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

import javax.validation.constraints.Email;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Size;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.king.common.annotation.Excel;
import com.king.common.annotation.Excel.Type;
import com.king.common.annotation.xss.Xss;
import com.king.common.constant.Constants;
import com.king.framework.base.domain.YyBaseEntity;

/**
 * 用户对象 sys_user
 * 
 * @author king
 */
@TableName("sys_user")
public class SysUserEntity extends YyBaseEntity {
	
	/** 部门ID */
	@Excel(name = "机构id", type = Type.IMPORT)
	private Long orgid;

	/** 用户账号 */
	@Excel(name = "登录名称")
	private String userName;

	/** 用户昵称 */
	@Excel(name = "用户名称")
	private String nickName;
	
	@Excel(name = "用户类型")
	private String userType;

	/** 用户邮箱 */
	@Excel(name = "用户邮箱")
	private String email;

	/** 手机号码 */
	@Excel(name = "手机号码")
	private String phonenumber;

	/** 用户性别 */
	@Excel(name = "用户性别", readConverterExp = "0=男,1=女,2=未知")
	private String sex;

	/** 用户头像 */
	private String avatar;

	/** 密码 */
	@JsonIgnore
	private String password;

	/** 帐号状态（0正常 1停用） */
	@Excel(name = "帐号状态", readConverterExp = "0=正常,1=停用")
	private String status;//UserStatus


	/** 最后登录IP */
	@Excel(name = "最后登录IP", type = Type.EXPORT)
	private String loginIp;

	/** 最后登录时间 */
	@Excel(name = "最后登录时间", width = 30, dateFormat = "yyyy-MM-dd HH:mm:ss", type = Type.EXPORT)
	@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+08:00")
	private Date loginDate;
	
	
	private String memo;

	/** 部门对象 */
//	@Excels({ @Excel(name = "部门名称", targetAttr = "deptName", type = Type.EXPORT),
//			@Excel(name = "部门负责人", targetAttr = "leader", type = Type.EXPORT) })
//	@TableField(exist = false)
//	private SysDept dept;

	/** 角色对象 */
	@TableField(exist = false)
	private List<SysRoleEntity> roles;

	/** 角色组 */
	@TableField(exist = false)
	private Long[] roleIds;


	/** 角色ID */
	@JsonSerialize(using= ToStringSerializer.class)
	@TableField(exist = false)
	private Long roleId;
	

	@TableField(exist = false)
	private String orgName;
	
	@TableField(exist = false)
	private Long processUserGroupId;//流程用户组id

	public SysUserEntity() {

	}

	public SysUserEntity(Long id) {
		setId(id);
	}


	public boolean isAdmin() {
		return isAdmin(getId());
	}

	public static boolean isAdmin(Long userId) {
		return userId != null && Constants.USER_ADMIN_USERID == userId;
	}


	@JsonSerialize(using= ToStringSerializer.class)
	public Long getOrgid() {
		return orgid;
	}

	public void setOrgid(Long orgid) {
		this.orgid = orgid;
	}

	@Xss(message = "用户昵称不能包含脚本字符")
	@Size(min = 0, max = 30, message = "用户昵称长度不能超过30个字符")
	public String getNickName() {
		return nickName;
	}

	public void setNickName(String nickName) {
		this.nickName = nickName;
	}

	@Xss(message = "用户账号不能包含脚本字符")
	@NotBlank(message = "用户账号不能为空")
	@Size(min = 0, max = 30, message = "用户账号长度不能超过30个字符")
	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	@Email(message = "邮箱格式不正确")
	@Size(min = 0, max = 50, message = "邮箱长度不能超过50个字符")
	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	@Size(min = 0, max = 11, message = "手机号码长度不能超过11个字符")
	public String getPhonenumber() {
		return phonenumber;
	}

	public void setPhonenumber(String phonenumber) {
		this.phonenumber = phonenumber;
	}

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	public String getAvatar() {
		return avatar;
	}

	public void setAvatar(String avatar) {
		this.avatar = avatar;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public String getLoginIp() {
		return loginIp;
	}

	public void setLoginIp(String loginIp) {
		this.loginIp = loginIp;
	}

	public Date getLoginDate() {
		return loginDate;
	}

	public void setLoginDate(Date loginDate) {
		this.loginDate = loginDate;
	}

	public List<SysRoleEntity> getRoles() {
		return roles;
	}

	public void setRoles(List<SysRoleEntity> roles) {
		this.roles = roles;
	}

	@JsonSerialize(using= ToStringSerializer.class)
	public Long[] getRoleIds() {
		return roleIds;
	}

	public void setRoleIds(Long[] roleIds) {
		this.roleIds = roleIds;
	}

	public Long getRoleId() {
		return roleId;
	}

	public void setRoleId(Long roleId) {
		this.roleId = roleId;
	}


	public String getUserType() {
		return userType;
	}

	public void setUserType(String userType) {
		this.userType = userType;
	}

	public String getMemo() {
		return memo;
	}

	public void setMemo(String memo) {
		this.memo = memo;
	}

	public String getOrgName() {
		return orgName;
	}

	public void setOrgName(String orgName) {
		this.orgName = orgName;
	}

	public Long getProcessUserGroupId() {
		return processUserGroupId;
	}

	public void setProcessUserGroupId(Long processUserGroupId) {
		this.processUserGroupId = processUserGroupId;
	}

	@Override
	public String toString() {
		return "SysUser [id=" + getId() + ", groupid=" + getGroupid() + ", orgid=" + orgid + ", userName=" + userName
				+ ", nickName=" + nickName + ", email=" + email + ", phonenumber=" + phonenumber + ", sex=" + sex
				+ ", avatar=" + avatar + ", password=" + password + ", status=" + status 
				+ ", loginIp=" + loginIp + ", loginDate=" + loginDate + ", roles=" + roles
				+ ", roleIds=" + Arrays.toString(roleIds) +  ", roleId="
				+ roleId + "]";
	}

}
